package o;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Build.VERSION;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.widget.ImageView;
import com.huawei.hwphy2d.physical.util.BarrierParameters;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import java.io.File;
import java.lang.ref.WeakReference;
import java.lang.reflect.InvocationTargetException;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

public class erm implements ero {
    private static erm ˊ;
    private static final Executor ˎ = Executors.newFixedThreadPool(1, new b());
    private dx<String, Bitmap> ʻ;
    private Bitmap ʼ;
    private Context ˋ;
    protected boolean ˏ = false;
    private final Object ॱ = new Object();
    private Bitmap ᐝ;

    static class a extends dx<String, Bitmap> {
        protected /* synthetic */ int ˏ(Object obj, Object obj2) {
            return ˋ((String) obj, (Bitmap) obj2);
        }

        public a(int i) {
            super(i);
        }

        protected int ˋ(String str, Bitmap bitmap) {
            if (doe.ˋ()) {
                return bitmap.getByteCount() / WXMediaMessage.DESCRIPTION_LENGTH_LIMIT;
            }
            return (bitmap.getRowBytes() * bitmap.getHeight()) / WXMediaMessage.DESCRIPTION_LENGTH_LIMIT;
        }
    }

    static class b implements ThreadFactory {
        private final AtomicInteger ˏ;

        private b() {
            this.ˏ = new AtomicInteger(1);
        }

        public Thread newThread(Runnable runnable) {
            return new Thread(runnable, "LocalApkIcon AsyncTask #" + this.ˏ.getAndIncrement());
        }
    }

    static class c extends BitmapDrawable {
        private final WeakReference<d> ˎ;

        public c(Resources resources, Bitmap bitmap, d dVar) {
            super(resources, bitmap);
            this.ˎ = new WeakReference(dVar);
        }

        public d ˊ() {
            return (d) this.ˎ.get();
        }
    }

    class d extends AsyncTask<String, Void, Bitmap> {
        private final WeakReference<ImageView> ˊ;
        final /* synthetic */ erm ˋ;
        private String ॱ;

        protected /* synthetic */ Object doInBackground(Object[] objArr) {
            return ˊ((String[]) objArr);
        }

        protected /* synthetic */ void onPostExecute(Object obj) {
            ˋ((Bitmap) obj);
        }

        public d(erm o_erm, ImageView imageView) {
            this.ˋ = o_erm;
            this.ˊ = new WeakReference(imageView);
        }

        protected Bitmap ˊ(String... strArr) {
            this.ॱ = strArr[0];
            String valueOf = String.valueOf(this.ॱ);
            synchronized (this.ˋ.ॱ) {
                while (this.ˋ.ˏ && !isCancelled()) {
                    try {
                        this.ˋ.ॱ.wait();
                    } catch (InterruptedException e) {
                        Logger.e("LocalApkIcon", "doInBackground(String... params) " + e.toString());
                    }
                }
            }
            return this.ˋ.ॱ(valueOf, true);
        }

        protected void ˋ(Bitmap bitmap) {
            try {
                if (isCancelled()) {
                    bitmap = null;
                }
                ImageView ˏ = ˏ();
                if (bitmap != null && ˏ != null) {
                    ˏ.setImageBitmap(bitmap);
                }
            } catch (Throwable e) {
                Logger.ˋ("LocalApkIcon", "GetApkIconTask onPostExecute error", e);
            }
        }

        private ImageView ˏ() {
            ImageView imageView = (ImageView) this.ˊ.get();
            return this == erm.ˎ(imageView) ? imageView : null;
        }
    }

    private erm(Context context) {
        this.ˋ = context;
        this.ʻ = new a(ˎ((float) BarrierParameters.RESTITUTION));
        this.ᐝ = ern.ॱ(context.getResources().getDrawable(o.dky.a.ˏˏ));
    }

    public static synchronized ero ˏ() {
        ero o_ero;
        synchronized (erm.class) {
            if (ˊ == null) {
                ˊ = new erm(ContentManager.getInstant().getContext());
            }
            o_ero = ˊ;
        }
        return o_ero;
    }

    private static d ˎ(ImageView imageView) {
        if (imageView != null) {
            Drawable drawable = imageView.getDrawable();
            if (drawable instanceof c) {
                return ((c) drawable).ˊ();
            }
        }
        return null;
    }

    private static Drawable ॱ(Context context, String str) throws NameNotFoundException {
        return ˏ(context, context.getPackageManager().getApplicationInfo(str, 8320).sourceDir);
    }

    private static Drawable ˏ(Context context, String str) {
        Drawable drawable = null;
        try {
            ApplicationInfo ॱ = ॱ(str);
            if (!(ॱ == null || ॱ.icon == 0)) {
                AssetManager ˎ = ˎ(str);
                Resources resources = context.getResources();
                drawable = new Resources(ˎ, resources.getDisplayMetrics(), resources.getConfiguration()).getDrawable(ॱ.icon);
            }
        } catch (NoSuchMethodException e) {
        } catch (Exception e2) {
        }
        return drawable;
    }

    public static ApplicationInfo ॱ(String str) throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchFieldException {
        Object newInstance;
        Class cls = Class.forName("android.content.pm.PackageParser");
        if (VERSION.SDK_INT >= 21) {
            newInstance = cls.getConstructor((Class[]) null).newInstance((Object[]) null);
            newInstance = cls.getDeclaredMethod("parsePackage", new Class[]{File.class, Integer.TYPE}).invoke(newInstance, new Object[]{new File(str), Integer.valueOf(0)});
        } else {
            newInstance = cls.getConstructor(new Class[]{String.class}).newInstance(new Object[]{str});
            new DisplayMetrics().setToDefaults();
            newInstance = cls.getDeclaredMethod("parsePackage", new Class[]{File.class, String.class, DisplayMetrics.class, Integer.TYPE}).invoke(newInstance, new Object[]{new File(str), str, r3, Integer.valueOf(0)});
        }
        if (newInstance != null) {
            return (ApplicationInfo) newInstance.getClass().getDeclaredField("applicationInfo").get(newInstance);
        }
        return null;
    }

    public static AssetManager ˎ(String str) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        AssetManager assetManager = (AssetManager) AssetManager.class.getConstructor(new Class[0]).newInstance(new Object[0]);
        AssetManager.class.getDeclaredMethod("addAssetPath", new Class[]{String.class}).invoke(assetManager, new Object[]{str});
        return assetManager;
    }

    public int ˎ(float f) {
        if (f >= 0.05f && f <= 0.8f) {
            return Math.round((((float) Runtime.getRuntime().maxMemory()) * f) / 1024.0f);
        }
        throw new IllegalArgumentException("setMemCacheSizePercent - percent must be between 0.05 and 0.8 (inclusive)");
    }

    public void ॱ(boolean z) {
        synchronized (this.ॱ) {
            this.ˏ = z;
            if (!this.ˏ) {
                this.ॱ.notifyAll();
            }
        }
    }

    public void ˎ(int i) {
        this.ʼ = BitmapFactory.decodeResource(this.ˋ.getResources(), i);
    }

    public void ˋ(ImageView imageView, String str) {
        if (!TextUtils.isEmpty(str)) {
            ॱ("GetLocalAppIcon_PackageName_" + str, imageView);
        }
    }

    public void ॱ(String str, ImageView imageView) {
        Bitmap bitmap = null;
        if (this.ʻ != null) {
            bitmap = (Bitmap) this.ʻ.ˏ(str);
        }
        if (bitmap != null) {
            imageView.setImageBitmap(bitmap);
        } else if (ˋ(str, imageView)) {
            d dVar = new d(this, imageView);
            imageView.setImageDrawable(new c(this.ˋ.getResources(), this.ʼ, dVar));
            dVar.executeOnExecutor(ˎ, new String[]{str});
        }
    }

    private boolean ˋ(String str, ImageView imageView) {
        d ˎ = ˎ(imageView);
        if (ˎ == null) {
            return true;
        }
        String ˎ2 = ˎ.ॱ;
        if (ˎ2 != null && ˎ2.equals(str)) {
            return false;
        }
        ˎ.cancel(true);
        return true;
    }

    public Bitmap ˎ(String str, boolean z) {
        return ॱ("GetLocalAppIcon_PackageName_" + str, z);
    }

    private Bitmap ॱ(String str, boolean z) {
        Bitmap ˋ;
        Drawable ॱ;
        Object obj = null;
        if (z) {
            try {
                ˋ = ˋ(str);
                if (ˋ != null) {
                    return ˋ;
                }
            } catch (OutOfMemoryError e) {
                Logger.e("LocalApkIcon", "getBitmap OutOfMemoryError");
                return null;
            }
        }
        Object obj2 = 1;
        try {
            if (str.startsWith("GetLocalAppIcon_PackageName_")) {
                try {
                    ॱ = ॱ(this.ˋ, str.substring("GetLocalAppIcon_PackageName_".length()));
                } catch (NameNotFoundException e2) {
                    obj2 = obj;
                    ॱ = null;
                }
            } else {
                ॱ = ˏ(this.ˋ, str);
            }
        } catch (OutOfMemoryError e3) {
            obj2 = obj;
            ॱ = null;
        }
        if (ॱ != null) {
            ˋ = ern.ˏ(this.ˋ, ern.ॱ(ॱ), z);
            if (z) {
                this.ʻ.ˋ(str, ˋ);
            }
            return ˋ;
        }
        if (obj2 != null) {
            this.ʻ.ˋ(str, this.ᐝ);
        }
        return this.ᐝ;
    }

    public Bitmap ˋ(String str) {
        if (this.ʻ != null) {
            Bitmap bitmap = (Bitmap) this.ʻ.ˏ(str);
            if (bitmap != null) {
                return bitmap;
            }
        }
        return null;
    }
}
